package cn.yh.register.order.api;

import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.AuthContextHolder;
import cn.yh.register.model.order.OrderInfo;
import cn.yh.register.order.service.OrderService;
import cn.yh.register.vo.order.OrderCountQueryVo;
import cn.yh.register.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderService orderService;


    @ApiOperation("预约下单")
    @PostMapping("/auth/submitOrder/{scheduleId}/{patientId}")
    public Result<Long> submitOrder(@PathVariable String scheduleId,
                                    @PathVariable Long patientId){
        Long orderId = orderService.saveOrder(scheduleId, patientId);
        return Result.ok(orderId);
    }

    @ApiOperation("分页获取用户订单")
    @GetMapping("/auth/{page}/{limit}")
    public Result<IPage<OrderInfo>> list(@PathVariable Integer page,
                         @PathVariable Integer limit,
                         OrderQueryVo orderQueryVo,
                         HttpServletRequest request){
        //设置用户id
         orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> orderPage = new Page<>(page, limit);
        IPage<OrderInfo> orderInfoIPage = orderService.selectPage(orderPage, orderQueryVo);
        return Result.ok(orderInfoIPage);
    }

    @ApiOperation("查看订单详情")
    @GetMapping("/auth/getOrder/{orderId}")
    public Result<OrderInfo> show(@PathVariable Long orderId){
        OrderInfo orderInfo = orderService.show(orderId);
        return Result.ok(orderInfo);
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("/auth/cancelOrder/{orderId}")
    public Result<?> cancelOrder(@PathVariable("orderId") Long orderId) {
        return Result.ok(orderService.cancelOrder(orderId));
    }

    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("/inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }

}
